<?php
/**
 * Created by JetBrains PhpStorm.
 * User: feizheng
 * Date: 12-7-10
 * Time: 下午4:11
 * To change this template use File | Settings | File Templates.
 */

class DateHelper
{
    function getSeoDate($articlesNum = 1000, $articesPerDay = 5, $date = "2012-12-25")
    {
        $sumDays = ceil($articlesNum / $articesPerDay);
        $randDate = array();
        $dateArr = $this->getDateArr($date);
        for ($i = 0; $i < $sumDays; $i++) {
            for ($j = 0; $j < $articesPerDay; $j++) {
                $randDate[] = $this->getFutureTime($dateArr);
            }
            $dateArr[2] += 1; //下个月
            $dateArr = $this->getPubDate($dateArr);
        }
        //array_multisort($randDate);
        return array_slice($randDate, 0, $articlesNum);
    }

    function getPubDate($date = "2011-01-13")
    {
        list($pub_year,$pub_month,$pub_day)=$this->getDateArr($date);
        $sumMonth = $this->getMonthDays($pub_month, $pub_year);
        if ($pub_day > $sumMonth) {
            $pub_day = 1;
            $pub_month++;
            //此处跨年
            if ($pub_month > 12) {
                $pub_year++;
                $pub_month = 1;
                $pub_day = 1;
            }
        }
        return array($pub_year, $pub_month, $pub_day);
    }


    function getMonthDays($month = 0, $year = 0)
    {
        if (!$month) return date('t');
        return cal_days_in_month(CAL_GREGORIAN, $month, $year);
    }

    function getDateArr($inStrDate = "2011-01-13", $inGetFullDate = false)
    {
        if(is_array($inStrDate)) return $inStrDate;
        $arrDate = getdate(strtotime(($inStrDate)));
        if (!$inGetFullDate) {
            $arrDate = array($arrDate['year'], $arrDate['mon'], $arrDate['mday']);
        }
        return $arrDate;
    }

    /**
     * @param $date
     * @param array $inHours
     * @return string
     */
    function getFutureTime($date, $inHours = array(7, 10))
    {
        $dateArr = $this->getDateArr($date);
        return sprintf(
            "%s-%s-%s %s:%s:%s",
            $dateArr[0],
            $dateArr[1],
            $dateArr[2],
            $this->rand2Time($inHours[0], $inHours[1]),
            $this->rand2Time(),
            $this->rand2Time());
    }

    /**
     * @param int $start
     * @param int $end
     * @param int $zero
     * @return string
     */
    function rand2Time($start = 1, $end = 59, $zero = 2)
    {
        return sprintf("%0{$zero}d", rand($start, $end));
    }
}